/**
 * Copyright (c) 2008 - TheNittyGritty - Thomas Brekelmans
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package nl.thenittygritty.list
{
	import nl.thenittygritty.iterator.IIterator;		

	/**
	 * An implementation of nl.thenittygritty.iteration.IIterator which should 
	 * be used to traverse through the data in a nl.thenittygritty.list.List.
	 */
	public class ListIterator implements IIterator
	{
		private var cursor:uint;
		private var listItems:Array;

		/**
		 * Creates a new ListIterator which will iterate over the given data.
		 * @param listItems	An Array, usually this iterator is instantiated in 
		 * nl.thenittygritty.list.List#getIterator, then this parameter should 
		 * be filled with this.items.  
		 */
		public function ListIterator(listItems:Array)
		{
			this.listItems = listItems;
			
			// Make sure we rewind the iterator before anyone starts using it.
			this.rewind();
		}

		/**
		 * Go back to the beginning of the list.
		 */
		public function rewind():void
		{
			this.cursor = 0;
		}

		/**
		 * Returns whether or not the list can be still be traversed. 
		 */
		public function isValid():Boolean
		{
			return (this.cursor >= 0 && this.cursor < this.listItems.length);
		}

		/**
		 * Tries to increase the position in the list by 1 and returns if 
		 * it succeeded.
		 */
		public function next():Boolean
		{
			var willBeValid:Boolean = (this.cursor + 1 < this.listItems.length);
			cursor++;
			
			return willBeValid;
		}

		/**
		 * Returns the item in the list which is currently processed.
		 */
		public function getValue():*
		{
			return this.listItems[this.cursor];
		}
		
		/**
		 * Returns the current cursor position.
		 */
		public function getCursor():uint
		{
			return this.cursor;
		}

		/**
		 * Returns the name of the class in a String.
		 */
		public function toString():String
		{
			return "ListIterator";
		}
	}
}